package com.yl.auth.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.HugongBean;
import com.yl.auth.entity.Users;
import com.yl.auth.mapper.UsersMapper;
import com.yl.auth.model.entity.Yuangong;
import com.yl.auth.mapper.YuangongMapper;
import com.yl.auth.model.vo.LaorenVo;
import com.yl.auth.model.vo.YuaGongPaiBanVo;
import com.yl.auth.model.vo.YuangongQueryVo;
import com.yl.auth.service.UsersService;
import com.yl.auth.service.YuangongService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-15
 */
@Service
public class YuangongServiceImpl extends ServiceImpl<YuangongMapper, Yuangong> implements YuangongService {
    @Resource
    private YuangongMapper yuangongMapper;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private UsersService usersService;
    @Override
    public List<Yuangong> findAll(YuangongQueryVo yuangong) {

        return yuangongMapper.findAll(yuangong);
    }

    @Override
    public IPage<Yuangong> findAllhugong(Page<Yuangong> page1, Yuangong yuangong) {
        return yuangongMapper.findAllhugong(page1,yuangong);
    }

    @Override
    public Yuangong getByIdhugong(Integer id) {
        Integer count=0;
        Yuangong Yuangong = yuangongMapper.selectByIdHugong(id);
        if(Yuangong.getLaorencount()>0){
            count=1;
        }
        Yuangong.setCount(count);
        return Yuangong;
    }

    @Override
    public void updateyuangong(Long id) {
        yuangongMapper.updateyuangong(id);
    }

    @Override
    public List<Yuangong> listPaiBan(YuaGongPaiBanVo yuaGongPaiBanVo) {
        return yuangongMapper.listPaiBan(yuaGongPaiBanVo);
    }

    @Override
    public IPage<Yuangong> getygList(Page<Yuangong> page, Yuangong yuangong) {
        return yuangongMapper.getygList(page,yuangong);
    }

    @Override
    public Yuangong getByName(String name) {
        return yuangongMapper.getByName(name);
    }

    @Override
    public void updateyuan(Integer id) {
        yuangongMapper.updateyuan(id);
    }

    @Override
    public List<Yuangong> getYuangong(YuangongQueryVo yuangong) {
        return yuangongMapper.find(yuangong);
    }

    @Override
    public Page<Yuangong> getAllAndPage(Integer page, Integer limit, Yuangong yuangong) {
        Page<Yuangong> page1=new Page<>(page,limit);
        Page<Yuangong> page2 = yuangongMapper.getAllAndPage(page1,yuangong);
        return page2;
    }

    @Override
    public Yuangong getByID(Long id) {

        return yuangongMapper.getbyId(id);
    }

    @Override
    public List<Integer> getBuMenById(Integer id) {
        return yuangongMapper.getBuMenById(id);
    }

    @Override
    public List<Integer> getGangWeiById(Integer usersId) {
        return yuangongMapper.getGangWeiById(usersId);
    }

    @Override
    public void savelaoren(Integer id1, Long id) {
        yuangongMapper.savelaoren(id1,id);
    }

    @Override
    public Yuangong login(Yuangong yuangong) {
        return yuangongMapper.login(yuangong);
    }

    @Override
    public Yuangong login(String account, String password) {
        return yuangongMapper.login(account,password);
    }

    @Override
    public void updatePassWord(Yuangong yuangong) {
        yuangongMapper.updatePassWord(yuangong);
    }

    @Override
    public void updateUsers(Yuangong yuangong) {
        if(yuangong!=null&&yuangong.getId()!=null){
            Users users=new Users();
            if(yuangong.getUsersId()==null){
                users.setUsername(yuangong.getAccount());
                users.setPassword(yuangong.getPassword());
                users.setImgurl(yuangong.getAvatar());
                users.setName(yuangong.getName());
                users.setPhone(yuangong.getPhone());
                users.setIdcard(yuangong.getIdcard());
                users.setType("3");
                usersMapper.insert(users);
                yuangong.setUsersId(users.getId());
                yuangongMapper.updateById(yuangong);
            }else {
                yuangongMapper.updateById(yuangong);
                usersMapper.updateByUid(yuangong);
            }
        }
    }


    @Override
    public void addXinzi(Yuangong yuangong) {
        if(yuangong!=null){
            yuangong.setMoney(2500.00);
            yuangongMapper.insertXinzi(yuangong);
        }
    }

    @Override
    public void deleteById(Long id,Yuangong yuangong) {
        if(id!=null){
            yuangongMapper.removeXinZi(id);
            yuangongMapper.removeUsers(yuangong);
        }
    }


}
